#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include "rngs.h"
#include <time.h>  
#include <stdlib.h>
#include <math.h>

const int KINGDOM_CARDS = 10;
const int NUM_CARD_TYPES = 27;
const char* cardName[] = {"curse", "estate", "duchy", "province", "copper", "silver", "gold", "adventurer",
							"council_room", "feast", "gardens", "mine", "remodel", "smithy", "village", "baron",
							"great_hall", "minion", "steward", "tribute", "ambassador", "cutpurse", "embargo", "outpost",
							"salvager", "sea_hag", "treasure_map"
							};

int main () {	
	//Initialization 
	int result = 0;
	int k[KINGDOM_CARDS];	

	int random_seed = rand() % 88888; 	//randomize the seed
	int numberOfPlayers = rand() % 4 + 2;  //randomize the number of players
             if (numberOfPlayers >= 5)
                 numberOfPlayers =2;
//Randomize the Kingdom cards.	
	for(int i = 0; i < KINGDOM_CARDS; i++) 
		k[i] = 0;
	int duplicate = 0;
	int z =0;
	while (z < KINGDOM_CARDS)
		{
		int card = rand() % 20 + 7;
		for(int i = 0; i < KINGDOM_CARDS; i++) {
			if(card == k[i]) duplicate = 1;
		}
		if(duplicate == 0) 
			{k[z] = card;
			  z = z++ ; }
	    duplicate = 0;
	}
	
	printf ("  Testing full game of Dominion.\n");
	printf ("\n");
	printf ("Random seed is : %d\n", random_seed);
	printf ("Number of players: %d\n", numberOfPlayers);
	printf ("Kingdom Cards Chosen: \n");
	for(int i = 0; i < KINGDOM_CARDS; i++) {
		printf("%s ", cardName[k[i]]);
	}
	printf("\n");
	printf("\n");

	struct gameState G;
	//Initialize game
	result = initializeGame(numberOfPlayers, k, random_seed, &G);

	//Play game
	int totalTurns = 0;
	int whose_turn = whoseTurn(&G);
	int hand_count;
	while(!isGameOver(&G)) {
		totalTurns++;
		whose_turn = whoseTurn(&G);

		printf("===TURN: %d === \n", totalTurns);
		
		printf("Player: %d\n", whose_turn);
		hand_count = G.handCount[whose_turn];
		printf("Cards in hand: ");
		for(int i = 0; i < G.handCount[whose_turn]; i++) {
			printf("%s ", cardName[G.hand[whose_turn][i]]);
		}
		printf("\n");
		printf("Discard pile Count: %d", G.discardCount[whose_turn]);
		printf("\n");

		int money = countMoney(G);
		
		printf("ACTION Phase-- \n");
		int actionCardCount = 0;
		int action_counter = 0;
		for(int i = 0; i < G.handCount[whose_turn]; i++)
			if(G.hand[whose_turn][i] > gold) 
				actionCardCount++;
					
		if(actionCardCount == 0) 
		printf("No Action cards in hand\n");
		else {
			int random_card = rand() % hand_count;
			int play_action_card = G.hand[whose_turn][random_card];
			int choice1 = 0; 
			int choice2 = 0; 
			int choice3 = 0;

		 switch(play_action_card){
			
				case feast:
					choice1 = rand() % KINGDOM_CARDS;
			
					break;
				case mine:
					choice1 = rand() % G.handCount[whose_turn];
					choice2 = rand() % 3 + 4;
					break;
				case remodel:
					choice1 = rand() % G.handCount[whose_turn];
					choice2 = rand() % treasure_map;
								break;
				case baron:
					choice1 = floor(Random() * 2);
					choice2 = -1;
					choice3 = -1;
					break;
				case minion:
					choice1 = rand() % 2 + 1;
					choice2 = -1;
					choice3 = -1;
					break;
				case steward:
					choice1 = rand() % 3 + 1;
						break;
				case ambassador:
					choice1 = rand() % G.handCount[whose_turn];
					choice2 = rand() % 2;
					choice3 = -1;
					break;
				case embargo:
					choice1 = rand() % KINGDOM_CARDS;
					break;
				case salvager:
					choice1 = rand() % G.handCount[whose_turn];
					choice2 = -1;
					choice3 = -1;
					break;
					
			
			}
			result = playCard(play_action_card, choice1, choice2, choice3, &G);
			if(result) printf("Failed to play %s.\n", cardName[play_action_card]);
			else {
				printf("The card %s is succesfully played.\n", cardName[play_action_card]);
				action_counter++;
			}

		}
		printf("Cards in hand: ");
		for(int i = 0; i < G.handCount[whose_turn]; i++) {
			printf("%s ", cardName[G.hand[whose_turn][i]]);
		}
		printf("\n");
		printf("The player is finished with actions.\n");

		printf("BUY Phase-- \n");
		int buy_random_card;
		int buy_kingdom_card;
		
	for(int i = 0; i < G.numBuys; i++) {
			buy_random_card = rand() % 18;
			buy_kingdom_card = k[buy_random_card];
		if (money >= 8) 
      {
        printf("player %d bought province\n",G.whoseTurn);
        buyCard(province, &G);
      }
      else if (money >= 6) 
      {
        printf("player %d bought # %d card\n",G.whoseTurn,buy_kingdom_card);
        buyCard(buy_kingdom_card, &G);
      }
      else if (money >= 3)
      {
        printf("player %d bought silver\n",G.whoseTurn);
        buyCard(silver, &G);
      }
      else if(money == 0)
      {
        printf("player %d bought copper\n",G.whoseTurn);
        buyCard(copper, &G);
      }
	  }
		
		printf("The player is finished buying\n");

		printf("CLEAN-UP Phase-- \n");
		endTurn(&G);
		printf("Discard pile Count: %d\n", G.discardCount[whose_turn]);
		printf("The player %d turn is over.\n", G.whoseTurn);
		printf("\n");
	}
	
	printf ("================================= \n");
	
	
    for(int i=0;i<27;i++){
                printf("Supply Count for [%d]: %d\n",i,G.supplyCount[i]);
    }

	printf ("================================= \n");

	int winner;
	int final_results[numberOfPlayers];
	printf("The game is now finished!\n");
	printf("The final score is: \n");
	for(int i = 0; i < numberOfPlayers; i++) {
		printf("Player %d: %d\n", i, scoreFor(i, &G));
	}

	getWinners(final_results, &G);
	for(int i = 0; i < numberOfPlayers; i++) 
		if(final_results[i] == 1) winner = i;
	printf("Player %d is the winner!\n", winner);

		return 0;
}
int countMoney(struct gameState G)
{
  int i, money = 0;
  for (i = 0; i < numHandCards(&G); i++) 
  {
    if (handCard(i, &G) == copper)
      money++;
    else if (handCard(i, &G) == silver)
      money += 2;
    else if (handCard(i, &G) == gold)
      money += 3;
  }
  return money;
}
